package com.wanmait.schedule.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.wanmait.schedule.entity.ClassMerge;
import com.wanmait.schedule.entity.Classes;
import com.wanmait.schedule.entity.Student;
import com.wanmait.schedule.entity.Teacher;
import com.wanmait.schedule.service.ClassMergeService;
import com.wanmait.schedule.service.ClassesService;
import com.wanmait.schedule.service.StudentService;
import com.wanmait.schedule.util.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 合班表 前端控制器
 * </p>
 *
 * @author wanmait
 * @since 2022-12-05
 */
@RestController
@RequestMapping("/manage/classMerge")
@RequiresPermissions("manage:classes")
public class ClassMergeController {
    @Resource
    private ClassMergeService classMergeService;
    @Resource
    private ClassesService classesService;
    @Resource
    private StudentService studentService;
    //列表
    @PostMapping("list")
    public AjaxResult list(@RequestBody ClassMerge classMerge,Integer pageNum){
        PageInfo<ClassMerge> pageInfo = classMergeService.list(classMerge,pageNum);
        return AjaxResult.success(pageInfo);
//        return AjaxResult.success("成功").put("list",classMergeService.list(classMerge,pageNum));
    }
    //合并班级
    @PostMapping("save")
    public AjaxResult save(@RequestBody ClassMerge classMerge, @RequestAttribute Teacher teacher){
        classMerge.setInputId(teacher.getId());
        classMergeService.save(classMerge);
        Classes byId = classesService.findById(classMerge.getMergeClassId());

        classMergeService.updateClassesStudentCount(byId.getStudentCount(),byId.getId(),classMerge.getNewClassId());
        return AjaxResult.success("保存成功");
    }
    //删除班级id
    @GetMapping("delete")
    public AjaxResult delete(Integer id){
        classMergeService.removeById(id);
        return AjaxResult.success("删除成功");
    }
    //根据id获得班级id信息
    @GetMapping("findById")
    public AjaxResult findById(Integer id){
        ClassMerge classMerge = classMergeService.findById(id);
        return AjaxResult.success("查询成功",classMerge);
    }
//  添加页列表
    @GetMapping("classesList")
    public AjaxResult findList(){
        List<Classes> list = classesService.list();
        return AjaxResult.success("成功").put("classesList",list);
    }
}
